<?php
    global $_GPC, $_W;
    $rid = intval($_GPC['id']);
        $uniacid = $_W['uniacid'];

        if(empty($rid)){
                message('活动规则参数错误！', '', 'error');
            }

        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_agent, 'MicroMessenger') === false) {

            header("HTTP/1.1 301 Moved Permanently");
            header("Location: {$this->createMobileUrl('other',array('type'=>1,'id'=>$rid))}");
            exit();
        }

        //    //网页授权借用开始

        load()->model('account');
        $_W['account'] = account_fetch($_W['acid']);
        $cookieid = '__cookie_haoman_dpm_201606186_' . $rid;
        $cookie = json_decode(base64_decode($_COOKIE[$cookieid]),true);
        if ($_W['account']['level'] != 4) {
            $from_user = $cookie['openid'];
            $avatar = $cookie['avatar'];
            $nickname = $cookie['nickname'];
        }else{
            $from_user = $_W['fans']['from_user'];
            $avatar = $_W['fans']['tag']['avatar'];
            $nickname = $_W['fans']['nickname'];
        }

        $code = $_GPC['code'];
        $urltype = '';
        if (empty($from_user) || empty($avatar) || empty($nickname)) {
            if (!is_array($cookie) || !isset($cookie['avatar']) || !isset($cookie['openid']) || !isset($cookie['nickname'])) {
                $userinfo = $this->get_UserInfo($rid, $code, $urltype);
                $nickname = $userinfo['nickname'];
                $avatar = $userinfo['headimgurl'];
                $from_user = $userinfo['openid'];
            } else {
                $avatar = $cookie['avatar'];
                $nickname = $cookie['nickname'];
                $from_user = $cookie['openid'];
            }
        }

        //网页授权借用结束

//       $from_user = 'oDWVZwq8sdLKLAh8YpRmBqCLoeMw';

        $page_from_user = base64_encode(authcode($from_user, 'ENCODE'));

       $countmoney_reply = pdo_fetch("select * from " . tablename('haoman_dpm_countmoneyreply') . " where rid = :rid order by `id` desc", array(':rid' => $rid));
//
        if (empty($countmoney_reply)) {
            message('非法访问，请重新发送消息进入活动页面！');
        }

        if($countmoney_reply['iscount']!=1){
            message('抱歉！活动还未开启，请留意大屏幕',$this->createMobileUrl('index',array('id'=>$rid)),'success');
        }
        if($countmoney_reply['countmoney_status']==1){
            if($countmoney_reply['status']==1){
                message('抱歉！活动已经开始，请留意大屏幕',$this->createMobileUrl('index',array('id'=>$rid)),'success');
            }elseif ($countmoney_reply['status']==2){
                message('抱歉！活动已经结束，请留意大屏幕',$this->createMobileUrl('index',array('id'=>$rid)),'success');
            }

        }

        if(empty($countmoney_reply['pici'])){
            pdo_update('haoman_dpm_countmoneyreply', array('pici' => 1), array('id' => $countmoney_reply['id']));
            $countmoney_reply['pici'] = 1;
        }
        if($countmoney_reply['countmoney_mannum']>0){
            $fans_num = pdo_fetchcolumn("SELECT count(id) FROM " . tablename('haoman_dpm_countmoneyuser') . " WHERE pici = :pici and  rid = " . $rid . " and uniacid=" . $uniacid . "",array(':pici'=>$countmoney_reply['pici']));

            if($fans_num>$countmoney_reply['countmoney_mannum']){
                message('抱歉！活动人数已经达上限，请留意大屏幕',$this->createMobileUrl('index',array('id'=>$rid)),'success');
            }
        }
        $fans = pdo_fetch("select id,avatar,nickname from " . tablename('haoman_dpm_fans') . " where rid = '" . $rid . "' and from_user='" . $from_user . "'");
        if ($fans == false) {
            header("HTTP/1.1 301 Moved Permanently");
            header("Location: " . $this->createMobileUrl('information', array('id' => $rid)) . "");
            exit();
        }


    $user = pdo_fetch( " SELECT id FROM ".tablename('haoman_dpm_countmoneyuser')." WHERE rid='".$rid."' and pici =  '".$countmoney_reply['pici']."' and from_user = '".$from_user."'" );

    if(empty($user)){

        $insert = array(
            'rid' => $rid,
            'uniacid' => $_W['uniacid'],
            'pici' => intval($countmoney_reply['pici']),
            'from_user' => $from_user,
            'avatar' => $fans['avatar'],
            'nickname' => $fans['nickname'],
            'count' => 0,
            'realname' => $countmoney_reply['countmoney_maxnum'],
            'is_back' => $fans['is_back'],
            'createtime' => time(),
        );
        pdo_insert('haoman_dpm_countmoneyuser', $insert);

    }


//分享信息
    $sharelink = $_W['siteroot'] . 'app/' . $this->createMobileUrl('messagesindex', array('id' => $rid, 'from_user' => $page_from_user));
    $sharetitle = empty($reply['share_title']) ? '一起来聊一聊吧!' : $reply['share_title'];
    $sharedesc = empty($reply['share_desc']) ? '亲，一起来聊一聊吧！！' : str_replace("\r\n", " ", $reply['share_desc']);
    if (!empty($reply['share_imgurl'])) {
        $shareimg = toimage($reply['share_imgurl']);
    } else {
        $shareimg = toimage($reply['picture']);
    }
    $jssdk = new JSSDK();
    $package = $jssdk->GetSignPackage();
    include $this->template('4426');